package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustomException;
import com.heima.model.behavior.dto.ApArticleRelationDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.behavior.pojo.ApUnlikesBehavior;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TheCai
 * @date 2021/6/4 23:12
 **/
@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {
    @Autowired
    ApLikesBehaviorService apLikesBehaviorService;
    @Autowired
    ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    /**
     * 查询用户文章的点赞\不喜欢
     * @param dto
     * @return
     */
    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        boolean islike =false;
        boolean isunlike =false;
        Map resultMap = new HashMap<>();
        resultMap.put("islike", false);
        resultMap.put("isunlike", false);


        //1.校验参数
        if (dto.getArticleId()==null||dto.getEntryId()==null){
            //没有就不用查了
            return resultMap;
        }

        //2.根据参数查询用户实体
//        ApUser user = AppThreadLocalUtils.getUser();
//        if (user==null||)
        ApBehaviorEntry apBehaviorEntry = new ApBehaviorEntry();
        if (dto.getType().intValue()==0){
        //按照设备id去查询行为实体
            apBehaviorEntry=  apBehaviorEntryService.findByUserIdOrEquipmentId(null,dto.getEntryId());
        }else{
      apBehaviorEntry =
          apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(), null);
        }
        //3.根据实体 查询是否点过赞
    if (apBehaviorEntry==null){
        //没有就不用查了
        return resultMap;
    }
        ApLikesBehavior apLikesBehavior = apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId()));
        if (apLikesBehavior!=null){
            resultMap.put("islike", true);
        }
        //4.TODO 根据实体 查询不喜欢数据(还没写不喜欢计次)
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId()));
        if (apUnlikesBehavior!=null){
            resultMap.put("isunlike", true);
        }
        //5.封装map  返回 是否点赞  是否不喜欢  及行为实体 ID
        //返回行为实体 ID ,在收藏行为是使用
        resultMap.put("entryId", apBehaviorEntry.getId());
        return resultMap;
    }
}
